package com.example.plane;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import com.example.plane.GameView.GameView;

import java.io.Serializable;

import static android.view.MotionEvent.ACTION_DOWN;
import static android.view.MotionEvent.ACTION_MOVE;


public class GameActivity extends AppCompatActivity {
    private static Context Context = null;
   public static WindowManager wm;
   public static int Scrrenwidth;
   public static int Screenheight;
    public static GameActivity instance=null;
    public static Resources resourcesInstance;
    public Bundle InstanceState;
    public static boolean gamepasue=false;
    public static boolean canre=false;
   public  GameView gameview;
   public  Serializable mDataset;
   public int touchx,touchy,touchmovex,touchmovey,touchcx,touchcy;
   public int bundlen=0,bundles,bundlew=0,bundlee;
   private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        resourcesInstance=this.getResources();
        wm = (WindowManager) this.getSystemService(this.WINDOW_SERVICE);//获取场景以及服务
        init();//初始化屏幕信息
        //读取数据
        gameview= new GameView(this);
        setContentView(gameview);
        listen();
    }
    public void init()
    {
        Screenheight=wm.getDefaultDisplay().getHeight();
        Scrrenwidth=wm.getDefaultDisplay().getWidth();
        bundlee=Scrrenwidth;
        bundles=Screenheight;
        //是否加载存档，后期设置为一个放法，并且用一个activity
        if(MenuActivity.game_state==MenuActivity.CONTIINUE_START) {
            SharedPreferences sp = getSharedPreferences("存档1", Context.MODE_PRIVATE);
            int bgmusictime = sp.getInt("bgmusic", 0);
            MainActivity.mediaPlayer[1].seekTo(bgmusictime);
        }else{ MainActivity.mediaPlayer[1].seekTo(0);}
        MainActivity.mediaPlayer[1].start();
    }
    public void onBackPressed()
    {
//     GameView.surfaceAvailable=false;
//        Intent intent = new Intent(GameActivity.this,MenuActivity.class);
        MainActivity.mediaPlayer[1].pause();
        GameView.state_pasue=true;
        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setTitle("暂停");
        builder.setMessage("游戏已暂停？");
        builder.setPositiveButton("退出游戏", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                    save_state();//应该保存的数据
                    GameActivity.super.onBackPressed();//注释掉这行,back键不退出activity
                    gameview.state_pasue=false;
                   Intent intent = new Intent(GameActivity.this,MenuActivity.class);//打开下一个活动
                   startActivity(intent);
                    finish();


            }
        });
        builder.setNegativeButton("继续游玩", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                MainActivity.mediaPlayer[1].start();
                GameView.state_pasue=false;
            }
        });
        builder.show();
    }
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("dataset", mDataset);
    }
    protected void onRestoreInstanceState(Bundle savedState) {
        super.onRestoreInstanceState(savedState);
        mDataset =  savedState.getSerializable("dataset");
    }
protected void save_state()
  {
////创建一个SharePreferences接口的实例对象，将生成一个XML名称为demo_01，模式为MODE_PRIVATE
          sp=this.getSharedPreferences("存档1",MODE_PRIVATE);
////通过edit()方创建一个SharePreferences.Editor类的实例对象
     SharedPreferences.Editor editor =sp.edit();
////通过putString()方法，将数据存入文件中
//       editor.putString("game_background_position_x", String.valueOf( gameview.ba.currX[0]));
//       editor.putString("game_background_position_Y", String.valueOf( gameview.ba.currY[0]));
         editor.putInt("bgmusic",MainActivity.mediaPlayer[1].getCurrentPosition());
////       editor.putString("name","sssss");
////       editor.putString("name","sssss");
////用commit()方法予以正式提交
           editor.commit();
   }
//   protected  void get_state()
//   {
//       gameview.ba.currX
//
//
    public  void listen(){
        gameview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_DOWN){
                    touchx= (int) event.getX();
                    touchy= (int) event.getY();
                    touchcx=touchx-gameview.heroplane.hpx;
                    touchcy=touchy-gameview.heroplane.hpy;
                    return  true;
                }
                if(event.getAction() == MotionEvent.ACTION_MOVE){
                    touchmovex= (int) event.getX();
                    touchmovey= (int) event.getY();
                    if((touchmovex- touchcx)>0&&(touchmovex- touchcx)<(Scrrenwidth-gameview.heroplane.bitmap.getWidth())&&(touchmovey- touchcy)>0&&(touchmovey- touchcy)<(Screenheight-gameview.heroplane.bitmap.getHeight())) {
                        gameview.heroplane.hpx = touchmovex - touchcx;
                        gameview.heroplane.hpy = touchmovey - touchcy;
                    }
                     return  true;
                }
                return true;
            }
        });
    }

    public int absoulutly(int i){
        if(i<0){
            return  -i;
        }else {
            return i;
        }
    }
 }



